//导入APP版本信息
import {
	_appVer
} from '../../main.js';
import {
	_verContent
} from '../../main.js';
import {
	_createTime
} from '../../main.js';

export default {
	data() {
		return {
			userId: '',
			userUuid: '',
			headImg: '',
			nickName: '',
			phone: '',
			email: '',
			dataLocation: '',
			password: '',
			//新密码
			newPassword1: '',
			//确认新密码
			newPassword2: '',

			current: 0,

			//是否显示用户信息更新页面
			updateUserShow: false,
			
			current: 0,
			downList: [{
					iconPath: "file-text",
					selectedIconPath: "file-text-fill",
					text: '文件',
					customIcon: false,
					pagePath: '/pages/index/index'
				},
				{
					iconPath: "edit-pen",
					selectedIconPath: "edit-pen-fill",
					text: '笔记',
					customIcon: false,
					pagePath: '/pages/notepad/notepad'
				},
				{
					iconPath: "account",
					selectedIconPath: "account-fill",
					text: '我的',
					customIcon: false,
					pagePath: '/pages/user/user'
				}
			],
		}
	},
	onShow() {
		this.headImg = uni.getStorageSync('headImg');
		this.nickName = uni.getStorageSync('nickName');
		this.userId = uni.getStorageSync('userId');
		this.userUuid = uni.getStorageSync('userUuid');
		this.phone = uni.getStorageSync('phone');
		this.email = uni.getStorageSync('email');
		this.dataLocation = uni.getStorageSync('dataLocation');
	},
	methods: {
		async beforeSwitch(index) {
			switch (index) {
				case 0:
					break;
				case 1:
					break;
				case 2:
					break;
			}
			return true;
		},
		//退出登录
		returnLogin() {
			uni.setStorage({
				key: 'userId',
				data: '',
				success: function() {

				}
			});
			uni.setStorage({
				key: 'userUuid',
				data: '',
				success: function() {

				}
			});
			uni.setStorage({
				key: 'phone',
				data: '',
				success: function() {

				}
			});
			uni.setStorage({
				key: 'email',
				data: '',
				success: function() {

				}
			});
			uni.setStorage({
				key: 'token',
				data: '',
				success: function() {

				}
			});
			uni.setStorage({
				key: 'headImg',
				data: '',
				success: function() {

				}
			});
			uni.setStorage({
				key: 'nickName',
				data: '',
				success: function() {

				}
			});
			uni.navigateTo({
				url: '/pages/login/login',
			});
		},
		//打开个人信息更新页面
		update() {
			this.$request('/User/queryUserById', {
				// 传参参数名：参数值,如果没有，就不需要传
				"userId": this.userId
			}).then(res => {
				if (res.code == 200) {
					this.phone = res.data.phone;
					this.nickname = res.data.nickname;
					this.headImg = res.data.headImg;
					this.updateUserShow = true;
				}
			})
		},
		//更新个人信息
		updateUser() {
			this.$request('/User/updateUserById', {
				// 传参参数名：参数值,如果没有，就不需要传
				"userId": this.userId,
				"nickname": this.nickname,
				"phone": this.phone,
				"headImg": this.headImg
			}).then(res => {
				if (res.code == 200) {
					uni.showToast({
						title: '修改成功',
						icon: 'none',
						duration: 2000
					});
					uni.setStorage({
						key: 'phone',
						data: this.phone,
						success: function() {

						}
					});
					uni.setStorage({
						key: 'headImg',
						data: this.headImg,
						success: function() {

						}
					});
					uni.setStorage({
						key: 'nickName',
						data: this.nickName,
						success: function() {

						}
					});
					this.updateUserShow = false;
				}
			})
		},
		//更新密码
		updatePassword() {
			
			if(this.newPassword1.length<6 || this.newPassword2.length<6 || this.password.length<6){
				uni.showToast({
					title: '密码格式出错',
					icon: 'none',
					duration: 2000
				});
				return;
			}
			if(this.newPassword1 != this.newPassword2){
				uni.showToast({
					title: '两次密码不一致',
					icon: 'none',
					duration: 2000
				});
				return;
			}
			this.$request('/User/updatePassword', {
				// 传参参数名：参数值,如果没有，就不需要传
				"userId": this.userId,
				"oldPassword": this.password,
				"newPassword": this.newPassword1
			}).then(res => {
				if (res.code == 200) {
					uni.showToast({
						title: '修改成功',
						icon: 'none',
						duration: 2000
					});
					this.newPassword1 = "";
					this.newPassword2 = "";
					this.password1 = "";
					this.updateUserShow = false;
				}
				else{
					uni.showToast({
						title: '修改失败',
						icon: 'none',
						duration: 2000
					});
					this.newPassword1 = "";
					this.newPassword2 = "";
					this.password1 = "";
					this.updateUserShow = false;
				}
			})
		},
		//上传图片
		upload() {
			uni.chooseImage({
				success: (chooseImageRes) => {
					const tempFilePaths = chooseImageRes.tempFilePaths;
					const uploadTask = uni.uploadFile({
						url: this.$url + '/Upload/uploadFile',
						filePath: tempFilePaths[0],
						name: 'file',
						formData: {
							'type': 'head'
						},
						success: (uploadFileRes) => {
							var data = JSON.parse(uploadFileRes.data)
							if (data.code == 200) {
								this.headImg = data.data.url;
								uni.showToast({
									title: '图片上传成功',
									icon: 'none',
									duration: 2000
								});
							} else {
								uni.showToast({
									title: '图片上传出错',
									icon: 'none',
									duration: 2000
								});
							}
						}
					});
				}
			});
		},
	}
}
